/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.web.controller;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.spring.web.servlet.view.JsonView;
import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang.StringUtils;
import org.cx1.model.Lead;
import org.cx1.model.utils.SystemProperties;
import org.cx1.service.api.IEmailService;
import org.cx1.service.api.ILeadService;
import org.cx1.service.impl.DLeadService;
import org.cx1.service.impl.MEmailService;
import org.cx1.service.impl.MLeadService;
import org.cx1.web.controller.action.LeadAction;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author ECH
 */
public class LeadControllerWS extends BaseController {
    
    private ILeadService leadService = new DLeadService();
    private IEmailService emailService = new MEmailService();
    
    public LeadControllerWS() {
        
    }
    
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
                                                 HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView();
        Map map = new HashMap();        
        
        Map<String, Object> parameterMap = new HashMap<String, Object>();
        if (ServletFileUpload.isMultipartContent(request)) {
            parameterMap = this.getMultipartFormParams(request);
        } 
        else {
            parameterMap = this.getFormParams(request);
        }
        
        Map<String, String> formfields = (Map<String, String>) parameterMap.get("formfields");
        int action = StringUtils.isEmpty(formfields.get("a")) ? 0 : Integer.parseInt(formfields.get("a"));
        
        switch (action) {         
            case LeadAction.CREATE: {
                create(mav, map, request);
                break;
            }
            case LeadAction.LEAD_VIEW: {
                leadView(mav, map, request);
                break;
            }
            default: {
                break;
            }
        }          
                
        map.put("host", SystemProperties.SYSTEM_HOST);
        map.put("port", SystemProperties.SYSTEM_PORT);
        
        mav.setView(new JsonView());
        mav.addObject("model", map);
        return mav;
    }    
    
    private void create(ModelAndView mav, Map map, HttpServletRequest request) {
        boolean success = false;
        
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");

        String term = request.getParameter("term");
        String type = request.getParameter("type");
        String city = request.getParameter("city");
        String neighborhood = request.getParameter("neighborhood");
        String structure = request.getParameter("structure");

        String bedsMin = request.getParameter("bedsMin");
        String bedsMax = request.getParameter("bedsMax");
        String priceMin = request.getParameter("priceMin");
        String priceMax = request.getParameter("priceMax");

        String description = request.getParameter("description");       
                                
        if (true) {
            Lead lead = new Lead();
            
            lead.setName(name);
            lead.setEmail(email);
            lead.setPhone(phone);
            
            lead.setTerm(term);
            lead.setType(type);
            
            lead.setCity(city);
            lead.setNeighborhood(neighborhood);
            lead.setStructure(structure);
            
            lead.setBedsMin(bedsMin);
            lead.setBedsMax(bedsMax);
            lead.setPriceMin(priceMin);
            lead.setPriceMax(priceMax);
            
            lead.setDescription(description);
            
            this.leadService.create(lead);
            this.emailService.sendLeadGenerated(name, email, description);
            success = true;
        } 
        else {
            success = false;
        }        
        map.put("success", success);
    }
    
    
    private void leadView(ModelAndView mav, Map map, HttpServletRequest request) {
        boolean success = false;        
        String name = request.getParameter("name");
        String email = request.getParameter("email");        
        String leadID = request.getParameter("leadID");
        emailService.sendLeadView(leadID, name, email);
        map.put("success", success);
    }
}